﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vowei.Data.Models;
using Vowei.Web.Models;
using Vowei.Data.Helpers;
using Vowei.Core.Models;
using Vowei.Data.Converters;

namespace Vowei.Web.Converters
{
    public class ProjectTimelineConverter : FormatConverter
    {
        private IVoweiContext _context;
        
        public ProjectTimelineConverter(IVoweiContext context)
            : base(DefaultTranslator)
        {
            _context = context;
        }

        public override object ConvertFrom(object project)
        {
            return ConvertFrom((Project)project);
        }

        public Timeline ConvertFrom(Project project)
        {
            var result = new Timeline();
            var timelines = new List<TimelineItem>();
            DateTime projectDue = project.DueDate.HasValue ? project.DueDate.Value : project.StartDate.Value + TimeSpan.FromDays(365 * 100);
            
            foreach (var task in project.AllTickets(_context).OfType<Task>())
            {
                if (task.StartDate.HasValue)
                {
                    timelines.Add(new TimelineItem()
                    {
                        Start = task.StartDate.Value,
                        End = task.DueDate,
                        Title = task.Title,
                        Description = task.Description
                    });
                }
            }

            // result.DateTimeFormat = "iso8601";
            result.Events = timelines.ToArray();
            return result;
        }

        public override void ConvertTo(object from, object to)
        {
            throw new NotImplementedException();
        }

        public override bool CanConvert(object from)
        {
            return from is Project;
        }
    }

}
